package com.sdp.lease.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.lease.cache.LeaseCleanCache;
import com.sdp.lease.db.l_supplier.entity.LSupplier;
import com.sdp.lease.db.l_supplier.service.ILSupplierService;
import com.sdp.lease.db.l_supplier_apply.entity.LSupplierApply;
import com.sdp.lease.db.l_supplier_apply.service.ILSupplierApplyService;
import com.sdp.lease.utils.CurrentLoginEmp;
import com.sdp.lease.utils.LeaseConstants;

import cn.hutool.core.bean.BeanUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-25
 */
@RestController
@RequestMapping("/LSupplierApplyController")
public class LSupplierApplyController {

	@Autowired
	private Query query;
	
	@Autowired
	private ILSupplierApplyService iLSupplierApplyService;
	
	@Autowired
	private ILSupplierService iLSupplierService;
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	@Autowired
	private LeaseCleanCache leaseCleanCache;
	
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iLSupplierApplyService, LSupplierApply.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody LSupplierApply supplierApply) {
		if(supplierApply==null) {
			return Result.error("申请息为空");
		}
		if(StringUtils.isEmpty(supplierApply.getSupplierAccountNo())) {
			return Result.error("申请人未登录");
		}
		if(StringUtils.isEmpty(StringUtils.isEmpty(supplierApply.getSupplierContact()) 
				|| StringUtils.isEmpty(supplierApply.getSupplierContactWay()))
				|| StringUtils.isEmpty(supplierApply.getSupplierAddress())
				|| StringUtils.isEmpty(supplierApply.getSupplierBusinessScope())
				|| StringUtils.isEmpty(supplierApply.getSupplierLoa())
				|| StringUtils.isEmpty(supplierApply.getSupplierLogo())) {
			return Result.error("申请人信息不完整");
		}
		//根据账号查询申请人是否已经申请过
		int count = iLSupplierService.querySupplierIsExist(supplierApply);
		if(count>0) {
			return Result.error("申请信息已存在，不能重复申请");
		}
		
		supplierApply.setApplyState(LeaseConstants.申请状态_申请中.getCode());
		supplierApply.setApplyTime(LocalDateTime.now());
		iLSupplierApplyService.save(supplierApply);
		return Result.ok();
	}
	
	/**
	 * 审批
	 * @param keyId
	 * @param applyState
	 * @return
	 */
	@GetMapping(value = "/approval")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> approval(@RequestParam(name="keyId",required=true) String keyId,@RequestParam(name="applyState",required=true) String applyState) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("审批信息不完整");
		}
		LSupplierApply supplierApply = iLSupplierApplyService.getById(keyId);
		if(supplierApply==null) {
			return Result.error("申请信息不存在");
		}
		if(!LeaseConstants.申请状态_申请中.getCode().equals(supplierApply.getApplyState())) {
			return Result.error("此申请已经审批过");
		}
		supplierApply.setApplyState(applyState);
		supplierApply.setApproverTime(LocalDateTime.now());
		supplierApply.setApproverNo(currentLoginEmp.getCurrentLoginInfo().getString("empNo"));
		supplierApply.setApproverName(currentLoginEmp.getCurrentLoginInfo().getString("empName"));
		iLSupplierApplyService.updateById(supplierApply);
		if(LeaseConstants.申请状态_申请通过.getCode().equals(applyState)) {
			LSupplier supplier = BeanUtil.copyProperties(supplierApply, LSupplier.class);
			supplier.setCreateTime(LocalDateTime.now());
			supplier.setKeyId(null);
			iLSupplierService.save(supplier);
		}
		leaseCleanCache.cleanSupplierCache();
		return Result.ok();
	}
}

